package com.mbyte.easy.rest.equapply;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Equapply;
import com.mbyte.easy.admin.service.IEquapplyService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@RestController
@RequestMapping("rest/equapply")
public class RestEquapplyController extends BaseController  {

    @Autowired
    private IEquapplyService equapplyService;

    /**
    * 查询列表
    *
    * @param pageNo
    * @param pageSize
    * @param equapply
    * @return
    */
    @RequestMapping
    public AjaxResult index(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String applyCreatetimeSpace, String applyUpdatetimeSpace, Equapply equapply) {
        Page<Equapply> page = new Page<Equapply>(pageNo, pageSize);
        QueryWrapper<Equapply> queryWrapper = new QueryWrapper<Equapply>();

        if(equapply.getApplyId() != null  && !"".equals(equapply.getApplyId() + "")) {
            queryWrapper = queryWrapper.like("apply_id",equapply.getApplyId());
         }


        if(equapply.getEquId() != null  && !"".equals(equapply.getEquId() + "")) {
            queryWrapper = queryWrapper.like("equ_id",equapply.getEquId());
         }


        if(equapply.getEquName() != null  && !"".equals(equapply.getEquName() + "")) {
            queryWrapper = queryWrapper.like("equ_name",equapply.getEquName());
         }


        if(equapply.getEquType() != null  && !"".equals(equapply.getEquType() + "")) {
            queryWrapper = queryWrapper.like("equ_type",equapply.getEquType());
         }


        if(equapply.getApplyPersonid() != null  && !"".equals(equapply.getApplyPersonid() + "")) {
            queryWrapper = queryWrapper.like("apply_personId",equapply.getApplyPersonid());
         }


        if(equapply.getApplyStatus() != null  && !"".equals(equapply.getApplyStatus() + "")) {
            queryWrapper = queryWrapper.like("apply_status",equapply.getApplyStatus());
         }


        if(equapply.getApplyReason() != null  && !"".equals(equapply.getApplyReason() + "")) {
            queryWrapper = queryWrapper.like("apply_reason",equapply.getApplyReason());
         }


        if(equapply.getApplyCreatetime() != null  && !"".equals(equapply.getApplyCreatetime() + "")) {
            queryWrapper = queryWrapper.like("apply_createtime",equapply.getApplyCreatetime());
         }


        if(equapply.getApplyUpdatetime() != null  && !"".equals(equapply.getApplyUpdatetime() + "")) {
            queryWrapper = queryWrapper.like("apply_updatetime",equapply.getApplyUpdatetime());
         }


        if(equapply.getApplyRemake() != null  && !"".equals(equapply.getApplyRemake() + "")) {
            queryWrapper = queryWrapper.like("apply_remake",equapply.getApplyRemake());
         }

        IPage<Equapply> pageInfo = equapplyService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("searchInfo",  equapply);
        map.put("pageInfo", new PageInfo(pageInfo));

        return this.success(map);
    }


    /**
    * 添加
    * @param equapply
    * @return
    */
    @PostMapping("add")
    public AjaxResult add(Equapply equapply){
        return toAjax(equapplyService.save(equapply));
    }

    /**
    * 添加
    * @param equapply
    * @return
    */
    @PostMapping("edit")
    public AjaxResult edit(Equapply equapply){
        return toAjax(equapplyService.updateById(equapply));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(equapplyService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(equapplyService.removeByIds(ids));
    }

}

